﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class Maker {
public:
	Maker() { // 构造函数 
		a = 10;
		cout << "构造函数" << endl;
	}
	~Maker() { // 析构函数 
		cout << "析构函数" << endl;
	}
public:
	int a;
};

void test01() {
	// 实例化对象做了两件事：1.分配空间；2.调用构造函数进行初始化 
	Maker m;
	int a = m.a;
	cout << "a=" << a << endl;
}

// 析构函数的作用 
class Maker2 {
public:
	Maker2(const char * name, int age) {
		cout << "有参构造" << endl;
		this->pName = (char*)malloc(strlen(name)+1);
		strcpy(pName, name);
		this->mAge = age;
	}
	~Maker2() {
		if (pName != NULL)
			free(pName);
	}
	void print() {
		cout << "姓名：" << pName << "，年龄：" << mAge << endl;
	}
private:
	char * pName;
	int mAge;
};

void test02() {
	Maker2 m2("翠花", 18);
	m2.print();
}

int main()
{
	// test01();
	test02();

	system("pause");
	return EXIT_SUCCESS;
}